package servlet;

import model.Essay;
import model.User;
import util.DBUtils;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserDelServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        int uid=Integer.parseInt(request.getParameter("fromUid"));
        System.out.println("UserDel:"+uid);
        DBUtils db = new DBUtils();
        db.delete("user","uid="+uid);

        List<Essay> essayList=new ArrayList();
        List<User> userList=new ArrayList();
        try {
            Connection conn=null;
            PreparedStatement ptmt=null;
            ResultSet rs=null;
            Class.forName("com.mysql.cj.jdbc.Driver");//注册数据库驱动
            String url = "jdbc:mysql://localhost:3306/boke?useSSL=false&serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf8";
            conn= DriverManager.getConnection(url, "root", "123456");//通过DriverManager获取数据库
            String  sql1="select e.*,u.username\n" +
                    "from essay e join `user` u\n" +
                    "on e.uid=u.uid\n";
            ptmt=conn.prepareStatement(sql1);
            rs = ptmt.executeQuery();
            while (rs.next()){
                Essay e=new Essay();
                e.setEid(rs.getInt("eid"));
                e.setTitle(rs.getString("title"));
                e.setCreate_time(rs.getTimestamp("create_time"));
                e.setUpdate_time(rs.getTimestamp("update_time"));
                e.setEclass(rs.getString("eclass"));
                e.setAuther(rs.getString("username"));
                essayList.add(e);
            }
            for (Essay e:essayList){
                System.out.println(e);
            }
            ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            String  sql2="select *\n" +
                    "from  `user` \n" ;
            ptmt=conn.prepareStatement(sql2);
            rs = ptmt.executeQuery();
            while (rs.next()){
                User U=new User();
                U.setUid(rs.getInt("uid"));
                U.setUsername(rs.getString("username"));
                U.setPassword(rs.getString("password"));
                U.setModifytime(rs.getTimestamp("modifytime"));
                U.setCreatetime(rs.getTimestamp("createtime"));
                U.setRole_id(rs.getInt("role_id"));
                userList.add(U);
            }
            for (User e:userList){
                System.out.println(e);
            }
            rs.close();
            ptmt.close();
            conn.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        request.getSession().setAttribute("essayList",essayList);
        request.getSession().setAttribute("userList",userList);
        RequestDispatcher rd = request.getRequestDispatcher("management_admin.jsp");
        rd.forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
